package org.jackysoft.entity;

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.jackysoft.util.MySqlSchema;
import org.jackysoft.util.NoSerialize;

@Entity
@Table(schema=MySqlSchema.Identifyedbase)
public class TreeNode extends LREntity implements Comparable<TreeNode> {


	/**
	 * 
	 */
	private static final long serialVersionUID = 2958614334609839007L;
	private String name;	
	private String uri;
	private Boolean visiable = true;
	
	private TreeNode parent;
	private Collection<AcegiRole> acegiRoles;		
    private boolean child = false;
	
	
	public TreeNode() {
	 
	}
	
	
	public TreeNode(String id,int lft,int rgt){
		super();
		this.id = id;
		this.lft = lft;
		this.rgt = rgt;
	}
	
	public TreeNode(String id, String name, String uri,	boolean visiable,boolean leaf,int sort,int lft,int rgt) {
		super();
		this.id = id;
		this.leaf = leaf;
		this.name = name;	
		this.uri = uri;
		this.visiable = visiable;
		this.sort = sort;
		this.lft = lft;
		this.rgt = rgt;
	}

	@Override
	@Transient
	public Boolean isLeaf() {
		return (rgt-lft)==1;
	}
	
	@NoSerialize
	@JSON(serialize = false)
	@ManyToMany(targetEntity = AcegiRole.class, mappedBy = "treeNodes")	
	public Collection<AcegiRole> getAcegiRoles() {
		return acegiRoles;
	}

	public void setAcegiRoles(Collection<AcegiRole> acegiRoles) {
		this.acegiRoles = acegiRoles;
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj || !(obj instanceof TreeNode) || id==null)
			return false;
		TreeNode o = (TreeNode) obj;
		if(o.getId()==null) return false;
		return o.getId().equals(this.getId());
	}

	@Override
	public int compareTo(TreeNode o) {
		if (!(o instanceof TreeNode) || o == null)
			return -1;
		if (this.sort > o.sort)
			return 1;
		else if (this.sort == o.sort)
			return 0;
		else if (this.sort < o.sort)
			return -1;
		else
			return 0;
	}

	public boolean isVisiable() {
		return visiable;
	}

	public void setVisiable(boolean visiable) {
		this.visiable = visiable;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}


	


	@Transient
	public TreeNode getParent() {
		return parent;
	}


	public void setParent(TreeNode parent) {
		this.parent = parent;
	}


	@Transient
	public boolean isChild() {
		return child;
	}


	public void setChild(boolean child) {
		this.child = child;
	}
	
}
